
import { AudioManager } from "../../frame/component/audio-manager";
import NativeClient from "../../frame/native/NativeClient";
import { GNCAPIURI, GNCBody, GNCResponse, GNCResult } from "./game.api.config.client";

type URIRequest = {[uri in GNCAPIURI]: {} extends GNCResult<uri> ? never : uri}[GNCAPIURI];
type URISend = {[uri in GNCAPIURI]: {} extends GNCResult<uri> ? uri : never}[GNCAPIURI];

export class GClientAPI {
	private static async request<uri extends URIRequest>(api: uri, data?: GNCBody<uri>, ccdata?: any){
		let struct = NativeClient.sendMessage(api, data, true, ccdata);
		return struct.response as GNCResponse<uri>;
	}

	private static send<uri extends URISend>(api: uri, data?: GNCBody<uri>){
		NativeClient.sendMessage(api, data, false);
	}

	/** 获取设备ID */
	public static async DeviceID(){
		let response = await GClientAPI.request(GNCAPIURI.DeviceID);
		return response.data.device_id;
	}

	/** 本地化接口 */
	public static async Local(){
		let response = await GClientAPI.request(GNCAPIURI.LOCAL);
		return response.data;
	}

	/** 广告请求 */
	public static async AD(type: GNCBody<GNCAPIURI.AD>['type']){
		GClientAPI.Float();
		AudioManager.pauseBgm();
		let response = await GClientAPI.request(GNCAPIURI.AD, {type}, mtec.string.random(5, 36));
		AudioManager.playBGM();
		return response.data.status == 'ad-over';
	}

	/** 设置剪贴板 */
	public static SetClipboard(text: GNCBody<GNCAPIURI.SetClipboard>['text']){
		GClientAPI.send(GNCAPIURI.SetClipboard, {text});
	}

	public static Float(){
		GClientAPI.send(GNCAPIURI.Float);
	}

	public static CloseLoading(){
		GClientAPI.send(GNCAPIURI.CloseLoading);
	}
}
